package com.yhxd.modular.news.entity;

import com.yhxd.modular.institutions.entity.Dept;
import com.yhxd.modular.system.entity.User;
import com.yhxd.utils.RedissonUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 信息
 */
@Data
@Entity(name = "news_news")
@EntityListeners(value = AuditingEntityListener.class)
public class News implements Serializable {

    @ApiModelProperty(value = "id")
    @Id
    @GeneratedValue(generator = "idGenerator")
    @GenericGenerator(name = "idGenerator", strategy = "com.yhxd.utils.IdUtils")
    @Column(name = "id")
    private Long id;

    @ApiModelProperty(value = "标题")
    @Column(name = "title")
    private String title;

    @ApiModelProperty(value = "信息分类id")
    @Column(name = "p_cid", insertable = false, updatable = false)
    private Long pcid;

    @ApiModelProperty(value = "信息类型")
    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "p_cid")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private Classify parentClassify;

    @ApiModelProperty(value = "信息小类id")
    @Column(name = "c_cid", insertable = false, updatable = false)
    private Long ccid;

    @ApiModelProperty(value = "信息小类")
    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "c_cid")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private Classify childClassify;

    @ApiModelProperty(value = "内容类型，0自定义，1本地上传")
    @Column(name = "con_type")
    private Integer conType;

    @ApiModelProperty(value = "自定义内容")
    @Column(name = "content")
    @Lob
    private String content;

    @ApiModelProperty(value = "本地上传pdf路径")
    @Column(name = "file_path")
    private String filePath;

    @ApiModelProperty(value = "本地上传pdf名称")
    @Column(name = "file_name")
    private String fileName;

    @ApiModelProperty(value = "本地上传pdf重命名")
    @Column(name = "new_file_name")
    private String newFileName;

    @ApiModelProperty(value = "本地上传pdf后缀")
    @Column(name = "file_suffix")
    private String fileSuffix;

    @ApiModelProperty(value = "开始日期")
    @Temporal(TemporalType.DATE)
    @Column(name = "begin_date")
    private Date beginDate;

    @ApiModelProperty(value = "结束日期")
    @Temporal(TemporalType.DATE)
    @Column(name = "end_date")
    private Date endDate;

    @ApiModelProperty(value = "是否重要公告，0否，1是")
    @Column(name = "important")
    private Integer important;

    @ApiModelProperty(value = "发布日期")
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "publish_date")
    private Date publishDate;

    @ApiModelProperty(value = "发布人")
    @Column(name = "publish_name")
    public String publishName;

    @ApiModelProperty(value = "发布人")
    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private User user;

    @ApiModelProperty(value = "发布机构id")
    @Column(name = "dept_id", insertable = false, updatable = false)
    private Long deptId;

    @ApiModelProperty(value = "发布机构")
    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private Dept dept;

    @ApiModelProperty(value = "状态，0新建，1发布，2撤销，3删除")
    @Column(name = "status")
    private Integer status;

    @ApiModelProperty(value = "创建人")
    @Column(name = "create_name")
    private String createName;

    @ApiModelProperty(value = "创建时间")
    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    @Column(name = "create_time")
    private Date createTime;

    @ApiModelProperty(value = "修改人")
    @Column(name = "update_name")
    private String updateName;

    @ApiModelProperty(value = "修改时间")
    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    @Column(name = "update_time")
    private Date updateTime;

    @ApiModelProperty(value = "数据是否可用，0不可用，1可用")
    @Column(name = "enable")
    private Integer enable = 1;

    @ApiModelProperty(value = "版本号")
    @Column(name = "version")
    private Integer version;

    @ApiModelProperty(value = "附件集合")
    @OneToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "news_id")
    @org.hibernate.annotations.ForeignKey(name = "none")
    private List<Attachment> attachmentList;

    @Transient
    private String statusStr;

    public News(){}

    public News(Integer status) {
        this.status = status;
    }

    public String getStatusStr() {
        Map<String, Map<String, String>> dictTypeMap = (Map<String, Map<String, String>>) RedissonUtils.getObject("dict");
        return dictTypeMap.get("info_status").get(String.valueOf(status));
    }

    @Transient
    private String importantStr;

    public String getImportantStr() {
        Map<String, Map<String, String>> dictTypeMap = (Map<String, Map<String, String>>) RedissonUtils.getObject("dict");
        return dictTypeMap.get("important_type").get(String.valueOf(important));
    }

    @Transient
    private String conTypeStr;

    public String getConTypeStr() {
        Map<String, Map<String, String>> dictTypeMap = (Map<String, Map<String, String>>) RedissonUtils.getObject("dict");
        return dictTypeMap.get("con_type").get(String.valueOf(conType));
    }
}
